package com.woniuxy.productweb.controller;


import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.lang.Snowflake;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.util.MyJwtUtil;
import com.woniuxy.core.util.PageInfo;
import com.woniuxy.productmodel.dto.TeamProductDto;
import com.woniuxy.productmodel.form.AddTeamProductForm;
import com.woniuxy.productmodel.form.ListTeamProductForm;
import com.woniuxy.productmodel.form.UpdateTeamProductForm;
import com.woniuxy.productmodel.form.VenueListTeamProductForm;
import com.woniuxy.productservice.exception.ProductException;
import com.woniuxy.productservice.service.TeamProductService;
import com.woniuxy.util.MyOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


/**
 * <p>
 * 团课产品表 前端控制器
 * </p>
 *
 * @author 冉杰
 * @since 2024-01-15
 */
@Api(tags = {"团课产品资源接口"})
@RestController
@RequestMapping("/teamProduct")
public class TeamProductController {
    @Autowired
    private TeamProductService teamProductService;
    @Autowired
    private MyOssUtil myOssUtil;

    /**
     * 商家端上传团课产品封面
     * @param uploadFile 图片文件jpg,png
     * @return
     * @throws IOException
     */
    @ApiOperation("商家端上传团课产品封面")
    @PostMapping("/upload.venue")
    public Result uploadProductImage(MultipartFile uploadFile) throws IOException {
        //上传封面
        String type = FileTypeUtil.getType(uploadFile.getInputStream());
        if (!type.equals("jpg")&&!type.equals("png")) throw new ProductException(70002,"图片格式异常");
        String fileName = new Snowflake(1, 1).nextIdStr();
        fileName = fileName+"."+type;
        String url = myOssUtil.uploadFile(uploadFile.getInputStream(), fileName, "ranjie11");
        return Result.success("商家端上传团课产品封面成功",url);
    }
    /**
     * 商家端新增团课产品
     * @param form 新增团课产品的参数对象
     * @param token 商家登录token
     * @return
     */
    @ApiOperation("商家端新增团课产品")
    @PostMapping("/add.venue")
    public Result<TeamProductDto> addTeamProduct(@RequestBody @Validated AddTeamProductForm form,@RequestHeader String token) throws IOException {
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
       TeamProductDto dto =  teamProductService.addTeamProduct(form,venueId);
       return Result.success("新增团课产品成功",dto);
    }

    /**
     * 商家端修改团课产品信息
     * @param form 修改团课产品信息参数对象
     * @param token 商家登录token
     * @return
     */
    @ApiOperation("商家端修改团课产品")
    @PostMapping("/update.venue")
    public Result updateTeamProduct(@RequestBody @Validated UpdateTeamProductForm form, @RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        teamProductService.updateTeamProduct(form,venueId);
        return Result.success("商家端修改团课课程产品成功");
    }

    /**
     * 商家端修改团课产品封面
     * @param updateFile 封面图片jpg png
     * @param token 商家登录token
     * @return
     */
    @ApiOperation("商家端修改团课产品封面")
    @PostMapping("/updateImage.venue")
    public Result updateProductImage(MultipartFile updateFile,Integer teamProductId,@RequestHeader String token) throws IOException {
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        teamProductService.updateProductImage(updateFile,teamProductId,venueId);
        return Result.success("商家修改团课产品封面成功");
    }

    /**
     * 根据产品id查找团课产品信息
     * @param teamProductId 产品id
     * @return
     */
    @ApiOperation("根据产品id查找团课产品信息")
    @GetMapping("/findById")
    public Result<TeamProductDto> findTeamProductById(Integer teamProductId){
        TeamProductDto dto = teamProductService.findTeamProductById(teamProductId);
        return Result.success("根据产品id查找团课产品信息",dto);
    }

    /**
     * 商家端查看自家团课产品信息
     * @param token 商家登录token
     */
    @ApiOperation("商家端查看自家团课产品信息")
    @PostMapping("/venueList.venue")
    public Result<PageInfo<TeamProductDto>> venueListTeamProduct(@RequestBody VenueListTeamProductForm form, @RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        PageInfo<TeamProductDto> pageInfo = teamProductService.venueListTeamProduct(form,venueId);
        return Result.success("商家端查看自家团课产品信息",pageInfo);
    }

    /**
     * 用户端条件查看团课产品信息
     */
    @ApiOperation("用户端条件查看团课产品信息")
    @PostMapping("/list")
    public Result<PageInfo<TeamProductDto>> listTeamProduct(@RequestBody ListTeamProductForm form){
        PageInfo<TeamProductDto> pageInfo = teamProductService.listTeamProduct(form);
        return Result.success("用户端条件查看团课产品信息",pageInfo);
    }

    /**
     * 商家端启用团课产品
     * @param teamProductId 产品id
     * @param token 商家登录token
     * @return
     */
    @ApiOperation("商家端启用团课产品")
    @GetMapping("/enable.venue")
    public Result enableTeamProduct(Integer teamProductId,@RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        teamProductService.enableTeamProduct(teamProductId,venueId);
        return Result.success("启用成功");
    }

    /**
     * 商家端禁用团课产品
     * @param teamProductId 产品id
     * @param token 商家登录token
     * @return
     */
    @ApiOperation("商家端禁用团课产品")
    @GetMapping("/disabled.venue")
    public Result disabledTeamProduct(Integer teamProductId,@RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        teamProductService.disabledTeamProduct(teamProductId,venueId);
        return Result.success("禁用成功");
    }

}

